<?php

namespace App\Admin\Controllers;

use App\Admin\Handers\WeChatHander;
use App\Models\Distributor;
use App\Models\DistributorLevel;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Admin\Selectable\Users;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Route;

class DistributorController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '分销员列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Distributor());
        
        $grid->column('name', __('Name'));
        $grid->column('mobile', __('Mobile'));
        $grid->column('promo_code', __('Promo code'));
        $grid->column('promo_code_url', __('Promo code url'))->image('', '60');
        $grid->column('distributorLevel.name', __('Distributor level id'));
        $grid->column('total_amount', __('Total amount'))->display(function ($data) {
            return '￥'.$data;
        })->sortable();
        $grid->column('usable_amount', __('Usable amount'))->display(function ($data) {
            return '￥'.$data;
        })->sortable();
        $grid->column('frozen_amount', __('Frozen amount'))->display(function ($data) {
            return '￥'.$data;
        })->sortable();
        $grid->column('used_amount', __('Used amount'))->display(function ($data) {
            return '￥'.$data;
        })->sortable();
        $grid->column('created_at', __('Created at'));
        
        $grid->filter(function ($filter) {
        	$filter->disableIdFilter();
        	$filter->like('name', __('Name'));
        	$filter->like('mobile', __('Mobile'));
        	$filter->equal('distributor_level_id', __('Distributor level id'))
	            ->select(DistributorLevel::all()->pluck('name', 'id'));
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Distributor::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('mobile', __('Mobile'));
        $show->field('promo_code', __('Promo code'));
        $show->field('promo_code_url', __('Promo code url'));
        $show->field('distributor_level_id', __('Distributor level id'));
        $show->field('total_amount', __('Total amount'));
        $show->field('usable_amount', __('Usable amount'));
        $show->field('frozen_amount', __('Frozen amount'));
        $show->field('used_amount', __('Used amount'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Distributor());

        // 分销员审核
        if($form->isEditing()){
            $form->text('name', __('Name'));
            $form->text('merchant_id', '平台商户号');
            $form->mobile('mobile', __('Mobile'));
            $form->text('promo_code', __('Promo code'))->disable();
            $form->image('promo_code_url', __('Promo code url'))->disable();
            
            $form->radio('distributor_level_id', __('Distributor level id'))
                ->options([1 => '分销员', 2 => '业务员']);
            
            $form->currency('total_amount', __('Total amount'))->symbol('￥');
            $form->currency('usable_amount', __('Usable amount'))->symbol('￥');
            $form->currency('frozen_amount', __('Frozen amount'))->symbol('￥');
            $form->currency('used_amount', __('Used amount'))->symbol('￥');
            
            $form->radio('reset_name', '重置二维码')->options([
            	1 => '是',
	            2 => '否'
            ]);
            
            $form->ignore(['reset_name']);
            
            $form->saved(function ($form){
                // 获取user_id
	            if(\request('reset_name') == 1){
	                $user = User::where('distributor_id', $form->model()->id)->first();
		            $weChat = new WeChatHander();
	                $form->model()->promo_code_url = $weChat->getPromoCodeUrl($user->id);
	                $form->model()->save();
	            }
	            
            });
        } else {
            // 指定业务员
            $form->radio('distributor_level_id', __('Distributor level id'))
                ->options([2 => '业务员'])->default(2);
            $form->belongsTo('user_id', Users::class, '用户名称');
            $form->text('name', __('Name'))->required();
            $form->text('merchant_id', '平台商户号');
            $form->mobile('mobile', __('Mobile'))->required();
            $form->text('promo_code', __('Promo code'))->disable();
            $form->image('promo_code_url', __('Promo code url'))->disable();
            $form->currency('total_amount', __('Total amount'))->symbol('￥');
            $form->currency('usable_amount', __('Usable amount'))->symbol('￥');
            $form->currency('frozen_amount', __('Frozen amount'))->symbol('￥');
            $form->currency('used_amount', __('Used amount'))->symbol('￥');
            
            $form->ignore(['user_id']);

            $form->saved(function ($form) {
            	//分销员推广码格式
                $id = $form->model()->id;
                $form->model()->promo_code = promoCode($id);
                
                // 生成分享用二维码
	            $user_id = \Request('user_id');
                $weChat = new WeChatHander();
                $form->model()->promo_code_url = $weChat->getPromoCodeUrl($user_id);
                $form->model()->save();
                
                // user表 pid 赋值 0
	            $user  = User::find($user_id);
	            $user->distributor_id = $form->model()->id;
	            $user->pid = 0;
	            $user->save();
            });
        }
        
        $form->disableCreatingCheck();
        $form->disableReset();

        return $form;
    }
    
    public function destroy($id)
    {
    	return $this->form()->destroy($id);
    }
}
